// pages/home/home.js

import {Theme} from "../../model/theme";
import {Banner} from "../../model/banner";
import {Category} from "../../model/category";
import {Activity} from "../../model/activity";
import date from "../../miniprogram_npm/lin-ui/common/async-validator/validator/date";
import {SpuPage} from "../../model/spu_page";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        topTheme1: null,
        theme2: null,
        theme4: null,
        banner1: null,
        banner2: null,
        categoryGrid: [],
        homeCoupon: null,
        theme2SpuList: [],
        spuPaging: null,
        loadMoreType:'loading'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        console.log("onLoad - options : " + options);
        this.initData();
    },

    async initData() {
        //首先获取所有的主题
        const theme = new Theme();
        await theme.getAllTheme();
        //const theme1Result = await Theme.getTopTheme1();
        const theme1Result = theme.getTheme1();
        const theme2Result = theme.getTheme2();
        //这里指的是有每周上新的时候去请求相关商品列表
        let theme2SpuList = [];
        if (theme2Result.online) {
            const date = await theme.getTheme2Spu();
            if (date) {
                //截取其中一段
                theme2SpuList = date.data.spu_list.slice(0, 8);
            }
        }

        const theme4Result = theme.getTheme4();
        //console.log(theme4Result);

        const banner1Result = await Banner.getBanner1();
        const banner2Result = await Banner.getBanner2();
        const categoryInfos = await Category.getCategoryInfos();
        const homeCoupon = await Activity.getHomeCoupon();

        this.initSpuPageList();
        /**
         * 将从服务器请求到的数据刷新至data中
         * 需要特别注意的返回的数据是什么格式的，请使用console.log()将其打印出来，然后赋予需要的数据值
         */
        this.setData({
            topTheme1: theme1Result,
            theme2: theme2Result,
            theme4: theme4Result,
            banner1: banner1Result.data,
            banner2: banner2Result.data,
            categoryGrid: categoryInfos.data,
            homeCoupon: homeCoupon.data,
            theme2SpuList: theme2SpuList
        });
    },

    async initSpuPageList() {
        const page = await SpuPage.getLatest();
        this.spuPaging = page;
        const data = await page.getMoreData();
        if (!data) {
            return;
        }
        console.log(data.items)
        wx.lin.renderWaterFlow(data.items);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        console.log("onReady");
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log("onShow");
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        console.log("onHide");
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console.log("onUnload");
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        console.log("onPullDownRefresh");
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: async function () {
        const data = await this.spuPaging.getMoreData()
        if (!data) {
            return;
        }
        wx.lin.renderWaterFlow(data.items)
        if(!data.hasNext){
            this.setData({
                loadMoreType:'end'
            });
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        console.log("onShareAppMessage");
    }
})